package com.example.androidtest;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class Main2Activity extends Activity {

	Button main2btn;

	ProgressBar pbar;
	myThresd mt=null;
	Button btn0001;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main2);
		 ViewServer.get(this).addWindow(this);
		
		initview();
	}
	
	
	
	 @Override
	    public void onDestroy() {
	    	super.onDestroy();
	    	ViewServer.get(this).removeWindow(this);
	    }
	    
	    @Override
	    public void onResume() {
	    	super.onResume();
	    	ViewServer.get(this).setFocusedWindow(this);
	    }
	
	
	
	
	
	
	
	
	
	
	
	private void initview(){
		main2btn=(Button)findViewById(R.id.main2btn);
		pbar=(ProgressBar)findViewById(R.id.main2pbar);
		pbar.setMax(100);
		
		
		
		main2btn.setOnClickListener(btnClick);
	}
	
	private void initPbarProcess(int progress){
		pbar.setProgress(progress);
	}
	
	private void StrartThread(){
//		mt.interrupt();
//		mt.isInterrupted();
//		mt.interrupted();

		
		if(mt==null){
			mt=new myThresd();
			mt.start();
		}
		
		
	}
	
	
	
	


	class myThresd extends Thread{

		@Override
		public void run() {
			try {
				
				for(int i=0;i<100;i++){
					Thread.sleep(1000);
					i=i+10;
					Message msg=new Message();
					msg.arg1=i;
					hanlder.sendMessage(msg);
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	
	
	
	
	
	Handler hanlder=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			
			super.handleMessage(msg);
			
			
			///msg.arg1=10;
			
			initPbarProcess(msg.arg1);
			
			
			
			
			
			
		}
		
	};
	
	
	
	
	
	
	
	
	OnClickListener btnClick=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.main2btn:
				pbar.setProgress(0);
				StrartThread();
				break;

			default:
				break;
			}
			
		}
	};
}
